package com.park.controller;

import com.park.common.bean.CommentInputView;
import com.park.common.bean.PageBean;
import com.park.common.bean.ResponseBean;
import com.park.common.constant.IDBConstant;
import com.park.common.exception.MessageException;
import com.park.common.poo.ReplyInfo;
import com.park.common.util.JsonUtils;
import com.park.platform.service.ICommentService;
import com.park.platform.service.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by leida on 2017/5/9.
 */
@RequestMapping("comment")
@Controller
public class CommentController extends BaseController {

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IDictService dictService;

    @RequestMapping("getComments")
    public String getComments(CommentInputView commentInputView, Model model){
        try {
            model.addAllAttributes(JsonUtils.fromJson(commentInputView));
            PageBean pageBean = commentService.getCommentsIN(commentInputView);
            super.setPageInfo(model, pageBean);

            model.addAttribute("commentTypes", dictService.getDictKeyValueListByName(IDBConstant.COMMENT_SERVICE_TYPE));
        }catch (Exception e){
            e.printStackTrace();
        }
        return "Comment/CommentList";
    }

    @RequestMapping("viewComment")
    public String viewComment(Integer commentId, Model model){
        if(commentId != null) {
            model.addAllAttributes(JsonUtils.fromJsonDF(commentService.getCommentInfoAndReplyInfo(commentId)));
        }
        return "Comment/CommentViews";
    }

    @ResponseBody
    @RequestMapping("saveReply")
    public ResponseBean saveReply(ReplyInfo replyInfo){
        try {
            replyInfo.setId(IDBConstant.CUSTOMER_SERVICE);
            commentService.saveCommentReply(replyInfo);
            return new ResponseBean(true);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

    @ResponseBody
    @RequestMapping("deleteComment")
    public ResponseBean deleteArticle(int commentId){
        try {
            commentService.newDeleteComment(commentId);
            return new ResponseBean(true);
        } catch (MessageException e) {
            e.printStackTrace();
            return new ResponseBean(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(false);
        }
    }

}
